<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>留言列表</title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css">
</head>
<body>
<nav class="navbar navbar-expand-lg navbar-dark bg-dark">
    <div class="container">
        <a class="navbar-brand" href="#">留言板系统</a>
        <div class="navbar-collapse">
            <ul class="navbar-nav me-auto">
                <li class="nav-item">
                    <a class="nav-link active" href="/messages">留言列表</a>
                </li>
            </ul>
            <div th:if="${session.currentUser != null}">
                欢迎：<span th:text="${session.currentUser.studentName}"></span>
            </div>
            <a href="/logout" class="btn btn-outline-light">退出</a>
        </div>
    </div>
</nav>

<div class="container mt-4">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <h2>留言列表</h2>
        <a href="/messages/add" class="btn btn-primary">发布新留言</a>
    </div>

    <div class="list-group">
        <div th:each="message : ${messages}" class="list-group-item">
            <div class="d-flex w-100 justify-content-between">
                <h3 class="mb-1" th:text="${message.title}"></h3>
                <small th:text="${#dates.format(message.writeDate, 'yyyy-MM-dd HH:mm')}"></small>
            </div>
            <p class="mb-1" th:text="${message.content}"></p>
            <small th:text="'发布者：' + ${message.writer}"></small>
            <div class="mt-2">
                <a th:href="@{/messages/{id}(id=${message.messageID})}" class="btn btn-sm btn-outline-primary">查看详情</a>
                <form th:if="${session.currentUser.studentName == message.writer}" th:action="@{/messages/{id}/delete(id=${message.messageID})}" method="post" class="d-inline">
                    <button type="submit" class="btn btn-sm btn-outline-danger ms-2">删除</button>
                </form>
            </div>
        </div>
    </div>
</div>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>